<?php

namespace app\mini\controller\v1\shop;

use app\common\library\exception\ApiException;
use app\common\library\MemberVo;
use app\common\library\Upload;
use app\common\logic\ServiceLogic;
use app\mini\controller\Common;
use think\Config;
use think\Env;
use think\Request;
use think\response\Json;

class Service extends Common
{

    public $ServiceLogic;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->ServiceLogic = new ServiceLogic();
    }

    /**
     * 获取商机类目
     * @return Json
     */
    public function index()
    {
        $res = $this->exception(function () {
            $params = request()->param();
            return $this->ServiceLogic->shop_service(isset($params['type']) ?? $params['type']);
        });
        return $this->response($res);
    }


    /**
     * 商机类别对应的筛选条件
     * @return Json
     */
    public function service_condition()
    {
        $res = $this->exception(function () {
            return $this->ServiceLogic->used_car(request()->param());
        });
        return $this->response($res);
    }


    /**
     * 非二手车信息添加与编辑
     * @return Json
     */
    public function edit()
    {
        $res = $this->exception(function () {
            return $this->ServiceLogic->edit(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 获取二手车详情
     * @return Json
     */
    public function car_detail()
    {
        $res = $this->exception(function () {
            return $this->ServiceLogic->car_detail(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 商机详情
     * @return Json
     */
    public function detail()
    {
        $res = $this->exception(function () {
            return $this->ServiceLogic->detail(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 商机上架
     * @return Json
     */
    public function put_up()
    {
        $res = $this->exception(function () {
            $params = request()->param();
            if ($params['service_type'] == 1){ //二手车
                $info = \app\common\model\UsedCar::get(['type'=>2,'shop_id'=>MemberVo::instance()->getShopId(),'id'=>$params['id']]);
            }else{
                $info = \app\common\model\ServiceShop::get(['type'=>2,'shop_id'=>MemberVo::instance()->getShopId(),'id'=>$params['id']]);
            }
            if (empty($info))
                throw new ApiException('无该条记录信息',404);
            if ($info->status !=1 )
                throw new ApiException('平台审核通过后才可以上架',404);
            $info->show = 1;
            $info->save();
            return true;
        });
        return $this->response($res);
    }

    /**
     * 商机下架
     * @return Json
     */
    public function put_down()
    {
        $res = $this->exception(function () {
            $params = request()->param();
            if ($params['service_type'] == 1){ //二手车
                $info = \app\common\model\UsedCar::get(['type'=>2,'shop_id'=>MemberVo::instance()->getShopId(),'id'=>$params['id']]);
            }else{
                $info = \app\common\model\ServiceShop::get(['type'=>2,'shop_id'=>MemberVo::instance()->getShopId(),'id'=>$params['id']]);
            }
            if (empty($info))
                throw new ApiException('无该条记录信息',404);
            $info->show = 2;
            $info->save();
            return true;
        });
        return $this->response($res);
    }

    /**
     * 商机删除
     * @return Json
     */
    public function del()
    {
        $res = $this->exception(function () {
            $params = request()->param();
            if ($params['service_type'] == 1){ //二手车
                $info = \app\common\model\UsedCar::get(['type'=>2,'shop_id'=>MemberVo::instance()->getShopId(),'id'=>$params['id']]);
            }else{
                $info = \app\common\model\ServiceShop::get(['type'=>2,'service_type'=>$params['service_type'],'shop_id'=>MemberVo::instance()->getShopId(),'id'=>$params['id']]);
            }
            if (empty($info))
                throw new ApiException('无该条记录信息',404);
            $info->is_del = 1;
            $info->save();
            return true;
        });
        return $this->response($res);
    }




}
